<?php

namespace app\index\controller;
use app\common\model\oms\StaffStore as StaffStoreModel;
use app\common\model\oms\Store as StoreModel;
use think\Validate;

class Store extends \app\common\controller\AuthBase {

    public function __construct() {
        parent::__construct();
    } 
 
    
    /**
     * 通过staff_id获取store_info
     */
    public function getStoreInfoByStaffId() {
        $params = $this->input();
        $staffStoreModel = new StaffStoreModel();
        $storeModel = new StoreModel(); 
        $store_id = $staffStoreModel->get_store_id($params['staff_id']);
        $store_info = $storeModel->get_store_info($store_id);
        return $this->output($store_info);
    }

    /**
     * 通过store_id获取store_info
     */
    public function getStoreInfoByStoreId() {
        $params = $this->input();
        $storeModel = new StoreModel(); 
        $store_info = $storeModel->get_store_info($params['store_id']);
        return $this->output($store_info);
    } 

    /**
     * 获取附近门店列表
     * @param type $coordinate(经纬度坐标)
     * @param type $mpid （mpId）
     * @param type $pageCondition (分页条件)
     * @param type $orderBy   （排序条件）
     */
    public function getStoreList(){
        $params = $this->input();
        $rule = [
            "coordinate|经纬度坐标" => 'require',//经纬度按逗号分隔的字符串
            "mp_id|公众号id"=>'require|number|max:11',
            'page|页码'=>'require|number',
            'pageSize|每页多少'=>'require|number|between:2,200',
            'order_by|排序'=>'max:50',
            'city_id|城市Id'=>'max:50',
            'region_id|地区Id'=>'max:100'
        ];
        $validate = new Validate($rule);
        if(!$validate->check($params)){
            return $this->output([], $validate->getError());
        } 
        
        $params['order_by']  = isset($params['order_by']) && !empty($params['order_by'])?$params['order_by']:'distance ASC';
        $params['city_id']   = isset($params['city_id']) && !empty($params['city_id'])?$params['city_id']:'';
        $params['region_id'] = isset($params['region_id']) && !empty($params['region_id'])?$params['region_id']:'';
        //查询附近门店列表
        $storeModel = new StoreModel();
        $data = $storeModel->Lists($params['coordinate'],$params['mp_id'], $params['page'],$params['pageSize'], $params['order_by'],  $params['city_id'], $params['region_id']);
        //判断适用门店是否存在
        if(!$data){
            return $this->output([], 'store not exist');
        }
        return $this->output($data,'success');
    }
    
}
